﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;

namespace Kotonoha.Server.Utils {
    public class ObservableProxyList<TObj, TProxy> : IList<TProxy>, INotifyCollectionChanged, IList
        where TProxy : class, INotifyPropertyChanged
      {
        private void InvokeCollectionChanged(NotifyCollectionChangedEventArgs e) {
            NotifyCollectionChangedEventHandler handler = CollectionChanged;
            if (handler != null) handler(this, e);
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        private readonly IList<TProxy> proxies;
        private readonly IList<TObj> list;
        private readonly Func<TProxy, TObj> unwrap;
        private readonly Func<TObj, TProxy> wrap;

        public ObservableProxyList(IList<TObj> list, Func<TObj, TProxy> wrap, Func<TProxy, TObj> unwrap) {
            this.list = list;
            this.wrap = wrap;
            this.unwrap = unwrap;
            proxies = list.Select(wrap).ToList();
        }

        public TProxy this[int idx] {
            get { return proxies[idx]; }
            set {
                TProxy old = proxies[idx];
                proxies[idx] = value;
                list[idx] = unwrap(value);
                InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, old, idx));
            }
        }

        public IEnumerator<TProxy> GetEnumerator() {
            return proxies.GetEnumerator();
        }

        public void Add(TProxy item) {
            proxies.Add(item);
            list.Add(unwrap(item));
            InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
        }

        public void Clear() {
            proxies.Clear();
            list.Clear();
            InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public bool Contains(TProxy item) {
            return proxies.Contains(item);
        }

        public void CopyTo(TProxy[] array, int arrayIndex) {
            proxies.CopyTo(array, arrayIndex);
        }

        public bool Remove(TProxy item) {
            bool removed = proxies.Remove(item);
            if (removed) {
                list.Remove(unwrap(item));
                InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
            }
            return removed;
        }

        public int Count {
            get { return proxies.Count; }
        }

        public bool IsReadOnly {
            get { return proxies.IsReadOnly; }
        }

        public int IndexOf(TProxy item) {
            return proxies.IndexOf(item);
        }

        public void Insert(int index, TProxy item) {
            proxies.Insert(index, item);
            list.Insert(index, unwrap(item));
            InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
        }

        public void RemoveAt(int index) {
            TProxy item = null;
            if (0 <= index && index < Count) {
                item = proxies[index];
            }
            proxies.RemoveAt(index);
            if (item != null) {
                list.RemoveAt(index);
                InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
            }
        }


        IEnumerator IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        void ICollection.CopyTo(Array array, int index) {
            this.CopyTo((TProxy[]) array, index);
        }

        int ICollection.Count {
            get { return Count; }
        }

        public object SyncRoot {
            get { return null; }
        }

        public bool IsSynchronized {
            get { throw new NotImplementedException(); }
        }

        public int Add(object value) {
            this.Add((TProxy) value);
            return this.Count - 1;
        }

        public bool Contains(object value) {
            return this.Contains((TProxy)value);
        }


        public int IndexOf(object value) {
            return this.IndexOf((TProxy) value);
        }

        public void Insert(int index, object value) {
            this.Insert(index, (TProxy) value);
        }

        public void Remove(object value) {
            this.Remove((TProxy) value);
        }


        object IList.this[int index] {
            get { return this[index]; }
            set { this[index] = (TProxy) value; }}
        

        public bool IsFixedSize {
            get { return false; }
        }
    }
}